package cn.iocoder.yudao.module.exam.service.uploadbatch;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.exam.controller.admin.uploadbatch.vo.*;
import cn.iocoder.yudao.module.exam.dal.dataobject.uploadbatch.UploadBatchDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.exam.dal.mysql.uploadbatch.UploadBatchMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.exam.enums.ExamErrorCodeConstants.UPLOAD_BATCH_NOT_EXISTS;

/**
 * 终端管理 Service 实现类
 *
 * @author Francis
 */
@Service
@Validated
public class UploadBatchServiceImpl implements UploadBatchService {

    @Resource
    private UploadBatchMapper uploadBatchMapper;

    @Override
    public Long createUploadBatch(UploadBatchSaveReqVO createReqVO) {
        // 插入
        UploadBatchDO uploadBatch = BeanUtils.toBean(createReqVO, UploadBatchDO.class);
        uploadBatchMapper.insert(uploadBatch);

        // 返回
        return uploadBatch.getId();
    }

    @Override
    public void updateUploadBatch(UploadBatchSaveReqVO updateReqVO) {
        // 校验存在
        validateUploadBatchExists(updateReqVO.getId());
        // 更新
        UploadBatchDO updateObj = BeanUtils.toBean(updateReqVO, UploadBatchDO.class);
        uploadBatchMapper.updateById(updateObj);
    }

    @Override
    public void deleteUploadBatch(Long id) {
        // 校验存在
        validateUploadBatchExists(id);
        // 删除
        uploadBatchMapper.deleteById(id);
    }

    @Override
        public void deleteUploadBatchListByIds(List<Long> ids) {
        // 删除
        uploadBatchMapper.deleteByIds(ids);
        }


    private void validateUploadBatchExists(Long id) {
        if (uploadBatchMapper.selectById(id) == null) {
            throw exception(UPLOAD_BATCH_NOT_EXISTS);
        }
    }

    @Override
    public UploadBatchDO getUploadBatch(Long id) {
        return uploadBatchMapper.selectById(id);
    }

    @Override
    public PageResult<UploadBatchDO> getUploadBatchPage(UploadBatchPageReqVO pageReqVO) {
        return uploadBatchMapper.selectPage(pageReqVO);
    }

}